﻿using Microsoft.Extensions.DependencyInjection;
using SD.TeachSystem.Common.Services;
using SD.TeachSystem.EntityFrameworkCore.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SD.TeachSystem.Common.LogTools 
{
    public static class GlobalTools
    {
        private static IServiceProvider _serviceProvider;

        // 初始化服务提供者，应该在程序启动时调用
        public static void Init(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        // 写入日志的方法
        public static async Task WriteLog(string methodName, string title, LogLevel logLevel, string message)
        {
            using var scope = _serviceProvider.CreateScope();
            var logService = scope.ServiceProvider.GetRequiredService<ISystemLogService>();

            var log = new SystemLogMes
            {
                MethodName = methodName, // 自定义方法名称
                Title = title,
                LogLevel = logLevel,
                Message = message,
            };

            // 异步写入日志
            await logService.WriteLogAsync(log);
        }
    }
}
